package com.inspectortime.webapp;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * 
 * @author mkvalsvik
 * 
 */
public class FindInspectorByZipFormController extends SimpleFormController {

	private static Log log = LogFactory
			.getLog(FindInspectorByZipFormController.class);

	public FindInspectorByZipFormController() {
		super();
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		return Collections.EMPTY_MAP;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		FindInspectorByZipForm form = new FindInspectorByZipForm();
		if (form.getRadius() == 0) {
			form.setRadius(30); // default	
		}		
		return form;
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		FindInspectorByZipForm form = (FindInspectorByZipForm) command;
		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?zip=" + form.getZipCode() + "&distance=" + form.getRadius());
		return mv;

	}

}
